package com.company.clientscard.main.widgets.adminForm;

import com.company.clientscard.main.widgets.general.TableManager;
import com.company.clientscard.shared.proxy.ClientProxy;
import com.company.clientscard.shared.proxy.UserProxy;
import com.company.clientscard.shared.proxyManager.ClientProxyManager;
import com.company.clientscard.shared.proxyManager.UserProxyManager;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class AdminForm extends FlexTable {
	final Label clientName = new Label("ADMIN login: admin@dobro.com.ua");

	final HorizontalPanel hp1 = new HorizontalPanel();
	final Label lClientName = new Label("Client name");
	final TextBox tClientName = new TextBox();
	final Label lClientCode = new Label("Client CODE");
	final TextBox tClientCode = new TextBox();
	final Button bAddClient = new Button("Add Client");

	final ClientsTable clientsTable;
	final TableManager<ClientProxy> clientsTableManager;
	ClientProxyManager clientProxyManager = new ClientProxyManager();

	final HorizontalPanel hp2 = new HorizontalPanel();
	final Label lLogin = new Label("Login");
	final TextBox tLogin = new TextBox();
	final Label lPassword = new Label("Password");
	final TextBox tPassword = new TextBox();
	final Button bAddUser = new Button("Add user");

	final UsersTable usersTable;
	final TableManager<UserProxy> usersTableManager;
	UserProxyManager userProxyManager = new UserProxyManager();

	public AdminForm() {
		clientsTableManager = new TableManager<ClientProxy>(clientProxyManager);
		clientsTable = new ClientsTable(clientsTableManager);
		// clientsTable = new ClientsTable(new
		// TableDataProvider<ClientProxy>());

		usersTableManager = new TableManager<UserProxy>(userProxyManager);
		usersTable = new UsersTable(usersTableManager);
		// usersTable = new UsersTable();

		hp1.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
		hp1.add(lClientName);
		hp1.add(tClientName);
		hp1.add(lClientCode);
		hp1.add(tClientCode);
		hp1.add(bAddClient);

		bAddClient.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ClientProxy newClient = clientProxyManager.getProxyObject();
				newClient.setCode(tClientCode.getText());
				newClient.setName(tClientName.getText());
				clientProxyManager.add(newClient, clientsTable
						.getTableManager().getDisplay());
			}
		});

		hp2.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
		hp2.add(lLogin);
		hp2.add(tLogin);
		hp2.add(lPassword);
		hp2.add(tPassword);
		hp2.add(bAddUser);

		bAddUser.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				UserProxy newUser = userProxyManager.getProxyObject();
				newUser.setLogin(tLogin.getText());
				newUser.setPassword(tPassword.getText());
				newUser.setRole("USER");
				userProxyManager.add(newUser, usersTable.getTableManager()
						.getDisplay());
			}
		});

		setWidth("100%");
		setWidget(0, 0, clientName);
		setWidget(1, 0, hp1);
		setWidget(2, 0, clientsTable);
		setWidget(3, 0, hp2);
		setWidget(4, 0, usersTable);

		getCellFormatter().setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
	}
}
